package com.bw.cart.server.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.bw.cart.common.domain.vo.CartVo;
import com.bw.common.core.domain.Result;
import com.bw.common.security.utils.SecurityUtils;
import com.bw.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bw.common.log.annotation.Log;
import com.bw.common.log.enums.BusinessType;
import com.bw.common.security.annotation.RequiresPermissions;
import com.bw.cart.common.domain.PmsCart;
import com.bw.cart.server.service.IPmsCartService;
import com.bw.common.core.web.controller.BaseController;
import com.bw.common.core.utils.poi.ExcelUtil;

/**
 * 购物车Controller
 * 
 * @author bw
 * @date 2024-12-27
 */
@RestController
@RequestMapping("/cart")
public class PmsCartController extends BaseController
{
    @Autowired
    private IPmsCartService pmsCartService;

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("cart:cart:list")
    @GetMapping("/list")
    public Result list(PmsCart pmsCart)
    {
        startPage();
        List<PmsCart> list = pmsCartService.selectPmsCartList(pmsCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("cart:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsCart pmsCart)
    {
        List<PmsCart> list = pmsCartService.selectPmsCartList(pmsCart);
        ExcelUtil<PmsCart> util = new ExcelUtil<PmsCart>(PmsCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @RequiresPermissions("cart:cart:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(pmsCartService.selectPmsCartById(id));
    }

    /**
     * 新增购物车
     */
    @RequiresPermissions("cart:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody PmsCart pmsCart)
    {
        return Result.success(pmsCartService.insertPmsCart(pmsCart));
    }

    /**
     * 修改购物车
     */
    @RequiresPermissions("cart:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody PmsCart pmsCart)
    {
        return Result.success(pmsCartService.updatePmsCart(pmsCart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("cart:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return Result.success(pmsCartService.deletePmsCartByIds(ids));
    }

    /**
     * 查询购物车
     */
    @GetMapping("/selectCartList")
    public Result selectCartList()
    {
        List<CartVo> cartVos = pmsCartService.selectCartList();
        return Result.success(cartVos);
    }

    /**
     * 添加购物车
     */
    @PostMapping("/addCart/{skuId}")
    public Result addCart(@PathVariable Long skuId)
    {
        pmsCartService.addCart(skuId);
        return Result.success();
    }

    /**
     * 根据skuId删除购物车
     * @param skuId
     * @return
     */
    @DeleteMapping("delCart/{skuId}")
    public Result delCart(@PathVariable Long skuId){
        pmsCartService.deleteCart(skuId);
        return Result.success();
    }

}
